package com.csdj.t309.service;

import com.csdj.t309.common.CacheContant;
import com.csdj.t309.common.Role;
import com.csdj.t309.entity.TSysUser;
import com.csdj.t309.entity.User;
import com.csdj.t309.entity.UserExample;
import com.csdj.t309.mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;

import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPubSub;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserService implements IUserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private RedisService redisService;

    private String name;

    private Logger log = Logger.getLogger("userService");

    @PostConstruct
    public void init(){
        redisService.subscribe("test", new JedisPubSub() {
            @Override
            public void onMessage(String channel, String message) {
                log.info("channel:"+channel+"   msg:"+message);
                try {
                    TimeUnit.SECONDS.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 带缓存的操作
     * @param id
     * @return
     */
    @Override
    public User getUserById(long id) {
        User user = redisService.getObj(CacheContant.USER_PREFIX +id,User.class);
        if(user == null){
            //从数据库中查询
            User userDb = userMapper.selectByPrimaryKey(id);
            if(userDb == null){
                return null;
            }
            //把数据保存到缓存
            redisService.setObj(CacheContant.USER_PREFIX+id,userDb,0);
            return userDb;
        }
        return user;
    }

    public List<TSysUser> getUsers(int roldId){
        return null;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Role(value = "#{username}")
    public User login(String username,String password){
        //省略加密
        UserExample example = new UserExample();
        example.createCriteria().andAccountEqualTo(username)
                .andPasswordEqualTo(password);
        List<User> users = userMapper.selectByExample(example);
        if(users != null && users.size()>0){
            return users.get(0);
        }
        return null;
    }

    List<User> getBypage(int page,int rows,Byte role){
        UserExample example = new UserExample();
        int offset = (page-1)*rows;
        example.setOffset(offset);
        example.setLimit(rows);
        example.setOrderByClause("id desc");
        if(role != null){
            example.createCriteria().andRoleIdEqualTo(role);
        }
        return userMapper.selectByExample(example);
    }

    long getUserTotal(Byte role){
        UserExample example = new UserExample();
        if(role != null){
            example.createCriteria().andRoleIdEqualTo(role);
        }
        return userMapper.countByExample(example);
    }


    @Override
    public void getTest(){
        TSysUser tSysUser = new TSysUser();
        tSysUser.setAddress("aaa");
        tSysUser.setPassword("aaaa");
        tSysUser.setAccount("aaaaaa");
        tSysUser.setRoleId(3L);
        tSysUser.setRealName("cloud");
//        int insert = userMapper.insert(User);
//        log.info("执行结果："+insert);
//        int c = 1/0;
    }

    /**
     * 被事务管理的方法
     * @return
     */
    @Override
    public boolean update() {
        getTest();
        return true;
    }
}
